package com.tgy.leetcode;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

/**
 * @Author: tgy
 * @Date: 12/8/20 11:47 AM
 *
 * https://leetcode-cn.com/problems/zui-chang-bu-han-zhong-fu-zi-fu-de-zi-zi-fu-chuan-lcof/
 *
 */
public class _剑指_Offer_48_最长不含重复字符的子字符串_01 {

    public int lengthOfLongestSubstring(String s) {

        if (s == null || s.length() == 0) {

            return 0;
        }

        char[] chars = s.toCharArray();

        int maxLen = 1;

        HashSet<Character> window = new HashSet<>();

        for (int i = 0, j = 0; i < chars.length ; i++) {

            char c = chars[i];

            while (window.contains(c)) {

                window.remove(chars[j++]);
            }

            window.add(c);

            maxLen = Math.max(maxLen,i - j + 1);
        }

        return maxLen;
    }
}
